package com.orangecy.common.feature;

import com.orangecy.common.bean.Member;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author : orangeCy.奕
 * @Time : 2021/9/24
 * @Product : 猎豹养成产品源码
 * 一个变量通过二进制位来表达多种语意
 */
public class BinaryBitUtil {

    private static final String TARGET_FLAG = "l_b_n_b";

    /**
     * 加入猎豹养成
     */
    private static final Integer JOIN_LEI_BAO_GROW = 1;
    /**
     * 退出猎豹养成
     */
    private static final Integer CANCEL_LEI_BAO_GROW = 1<<1;
    /**
     * 猎豹养成订阅到期
     */
    private static final Integer EXPIRED_LEI_BAO_GROW = 1<<2;

    public static void main(String[] args) {
        String originValue = "orangeCy";
        Member member = new Member(originValue);
        //打上加入猎豹养成标
        String s1 = addMark4JoinLeiBaoGrow(member.getFeatureStr());
        System.out.println(s1);
        //取消加入猎豹养成标
        String s2 = removeMark4JoinLeiBaoGrow(s1);
        System.out.println(s2);
    }

    private static String addMark4JoinLeiBaoGrow(String featureStr) {
        String originValue = FeatureStrUtil.getFeature(featureStr, TARGET_FLAG);
        if(StringUtils.isEmpty(originValue)) {
            return FeatureStrUtil.putFeature(featureStr, TARGET_FLAG, String.valueOf(JOIN_LEI_BAO_GROW));
        } else {
            Integer origin = Integer.valueOf(originValue);
            origin = origin | JOIN_LEI_BAO_GROW;
            return FeatureStrUtil.putFeature(featureStr, TARGET_FLAG, String.valueOf(origin));
        }
    }

    private static String removeMark4JoinLeiBaoGrow(String featureStr) {
        String originValue = FeatureStrUtil.getFeature(featureStr, TARGET_FLAG);
        if(StringUtils.isEmpty(originValue)) {
            return featureStr;
        } else {
            Integer origin = Integer.valueOf(originValue);
            origin = origin & ~JOIN_LEI_BAO_GROW;
            return FeatureStrUtil.putFeature(featureStr,TARGET_FLAG, String.valueOf(origin));
        }
    }
}